package com.transmit.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;


public class CmdShellUtil {

    private static final Logger logger = LoggerFactory.getLogger(CmdShellUtil.class);

    public static boolean runcmdOsgb(String cmd, File destfile, int count) throws Exception {
        //调用转换子进程
        Process process = Runtime.getRuntime().exec(cmd, null, destfile);
        //转换结果
        AtomicReference<AtomicBoolean> tempResult = new AtomicReference<>(new AtomicBoolean(false));
        //转换信息
        StringBuilder transInfo = new StringBuilder();
        //获取转换信息
        new Thread(() -> {
            BufferedReader info = new BufferedReader( new InputStreamReader(process.getInputStream()));
            String line;
            try {
                while ((line = info.readLine()) != null){
                    transInfo.append(line);
                    transInfo.append("\n");
                    if(line.toLowerCase().contains("info")){
                        logger.info("------------转换成功[" + count + "]------------");
                        tempResult.get().set(true);
//                        break;
                    }
                }
            }catch (Exception e){
                logger.error(e.getMessage());
            }
        }).start();
        new Thread(() -> {
            BufferedReader error = new BufferedReader( new InputStreamReader(process.getErrorStream()));
            String line;
            try {
                while ((line = error.readLine()) != null) {
                    transInfo.append(line);
                    transInfo.append("\n");
                    if(line.toLowerCase().contains("info")){
                        logger.info("------------转换成功[" + count + "]------------");
                        tempResult.get().set(true);
//                        break;
                    }
                }
            }catch (Exception e){
                logger.error(e.getMessage());
            }
        }).start();
        //等待转换子进程结束
        try {
            process.waitFor();
        } catch (InterruptedException e) {
            logger.error(e.getMessage());
        }
        //销毁转换进程
        process.destroy();
        //检测转换结果
        boolean result = tempResult.get().get();
        if(!result) {
            logger.error("------------转换失败[" + count + "]------------");
            //日志记录转换信息
            logger.error("------------转换信息[" + count + "]------------");
            logger.info(transInfo.toString());
            if(count == 1) {
                result = runcmdCloud(cmd, destfile, 2);
            }
        }else {
            //日志记录转换信息
            logger.error("------------转换信息[" + count + "]------------");
            logger.info(transInfo.toString());
        }
        //返回结果
        return result;
    }

    public static boolean runcmdGlb(String cmd, File destfile, int count) throws Exception {
        //调用转换子进程
        Process process = Runtime.getRuntime().exec(cmd, null, destfile);
        //转换结果
        AtomicReference<AtomicBoolean> tempResult = new AtomicReference<>(new AtomicBoolean(false));
        //转换信息
        StringBuilder transInfo = new StringBuilder();
        //获取转换信息
        new Thread(() -> {
            BufferedReader info = new BufferedReader( new InputStreamReader(process.getInputStream()));
            String line;
            try {
                while ((line = info.readLine()) != null){
                    transInfo.append(line);
                    transInfo.append("\n");
                    if(line.toLowerCase().contains("info")){
                        logger.info("------------转换成功[" + count + "]------------");
                        tempResult.get().set(true);
//                        break;
                    }
                }
            }catch (Exception e){
                logger.error(e.getMessage());
            }
        }).start();
        new Thread(() -> {
            BufferedReader error = new BufferedReader( new InputStreamReader(process.getErrorStream()));
            String line;
            try {
                while ((line = error.readLine()) != null) {
                    transInfo.append(line);
                    transInfo.append("\n");
                    if(line.toLowerCase().contains("info")){
                        logger.info("------------转换成功[" + count + "]------------");
                        tempResult.get().set(true);
//                        break;
                    }
                }
            }catch (Exception e){
                logger.error(e.getMessage());
            }
        }).start();
        //等待转换子进程结束
        try {
            process.waitFor();
        } catch (InterruptedException e) {
            logger.error(e.getMessage());
        }
        //销毁转换进程
        process.destroy();
        //检测转换结果
        boolean result = tempResult.get().get();
        if(!result) {
            logger.error("------------转换失败[" + count + "]------------");
            //日志记录转换信息
            logger.error("------------转换信息[" + count + "]------------");
            logger.info(transInfo.toString());
            if(count == 1) {
                result = runcmdCloud(cmd, destfile, 2);
            }
        }else {
            //日志记录转换信息
            logger.error("------------转换信息[" + count + "]------------");
            logger.info(transInfo.toString());
        }
        //返回结果
        return result;
    }

    public static boolean runcmdCloud(String cmd, File destfile, int count) throws Exception {
        //调用转换子进程
        Process process = Runtime.getRuntime().exec(cmd, null, destfile);
        //转换结果
        AtomicReference<AtomicBoolean> tempResult = new AtomicReference<>(new AtomicBoolean(false));
        //转换信息
        StringBuilder transInfo = new StringBuilder();
        //获取转换信息
        new Thread(() -> {
            BufferedReader info = new BufferedReader( new InputStreamReader(process.getInputStream()));
            String line;
            try {
                while ((line = info.readLine()) != null){
                    transInfo.append(line);
                    transInfo.append("\n");
                    if(line.toLowerCase().contains("[msg]:ok")){
                        logger.info("------------转换成功[" + count + "]------------");
                        tempResult.get().set(true);
//                        break;
                    }
                }
            }catch (Exception e){
                logger.error(e.getMessage());
            }
        }).start();
        new Thread(() -> {
            BufferedReader error = new BufferedReader( new InputStreamReader(process.getErrorStream()));
            String line;
            try {
                while ((line = error.readLine()) != null) {
                    transInfo.append(line);
                    transInfo.append("\n");
                    if(line.toLowerCase().contains("[msg]:ok")){
                        logger.info("------------转换成功[" + count + "]------------");
                        tempResult.get().set(true);
//                        break;
                    }
                }
            }catch (Exception e){
                logger.error(e.getMessage());
            }
        }).start();
        //等待转换子进程结束
        try {
            process.waitFor();
        } catch (InterruptedException e) {
            logger.error(e.getMessage());
        }
        //销毁转换进程
        process.destroy();
        //检测转换结果
        boolean result = tempResult.get().get();
        if(!result) {
            logger.error("------------转换失败[" + count + "]------------");
            //日志记录转换信息
            logger.error("------------转换信息[" + count + "]------------");
            logger.info(transInfo.toString());
            if(count == 1) {
                result = runcmdCloud(cmd, destfile, 2);
            }
        }else {
            //日志记录转换信息
            logger.error("------------转换信息[" + count + "]------------");
            logger.info(transInfo.toString());
        }
        //返回结果
        return result;
    }

    public static boolean runcmdJson(String cmd, File destfile, int count) throws Exception {
        //调用转换子进程
        Process process = Runtime.getRuntime().exec(cmd, null, destfile);
        //转换结果
        AtomicReference<AtomicBoolean> tempResult = new AtomicReference<>(new AtomicBoolean(false));
        //转换信息
        StringBuilder transInfo = new StringBuilder();
        //获取转换信息
        new Thread(() -> {
            BufferedReader info = new BufferedReader( new InputStreamReader(process.getInputStream()));
            String line;
            try {
                while ((line = info.readLine()) != null){
                    transInfo.append(line);
                    transInfo.append("\n");
                    if(line.toLowerCase().contains("success")){
                        logger.info("------------转换成功[" + count + "]------------");
                        tempResult.get().set(true);
//                        break;
                    }
                }
            }catch (Exception e){
                logger.error(e.getMessage());
            }
        }).start();
        new Thread(() -> {
            BufferedReader error = new BufferedReader( new InputStreamReader(process.getErrorStream()));
            String line;
            try {
                while ((line = error.readLine()) != null) {
                    transInfo.append(line);
                    transInfo.append("\n");
                    if(line.toLowerCase().contains("success")){
                        logger.info("------------转换成功[" + count + "]------------");
                        tempResult.get().set(true);
//                        break;
                    }
                }
            }catch (Exception e){
                logger.error(e.getMessage());
            }
        }).start();
        //等待转换子进程结束
        try {
            process.waitFor();
        } catch (InterruptedException e) {
            logger.error(e.getMessage());
        }
        //销毁转换进程
        process.destroy();
        //检测转换结果
        boolean result = tempResult.get().get();
        if(!result) {
            logger.error("------------转换失败[" + count + "]------------");
            //日志记录转换信息
            logger.error("------------转换信息[" + count + "]------------");
            logger.info(transInfo.toString());
            if(count == 1) {
                result = runcmdCloud(cmd, destfile, 2);
            }
        }else {
            //日志记录转换信息
            logger.error("------------转换信息[" + count + "]------------");
            logger.info(transInfo.toString());
        }
        //返回结果
        return result;
    }

    public static boolean runcmdCsv(String cmd, File destfile, int count) throws Exception {
        //调用转换子进程
        Process process = Runtime.getRuntime().exec(cmd, null, destfile);
        //转换结果
        AtomicReference<AtomicBoolean> tempResult = new AtomicReference<>(new AtomicBoolean(false));
        //转换信息
        StringBuilder transInfo = new StringBuilder();
        //获取转换信息
        new Thread(() -> {
            BufferedReader info = new BufferedReader( new InputStreamReader(process.getInputStream()));
            String line;
            try {
                while ((line = info.readLine()) != null){
                    transInfo.append(line);
                    transInfo.append("\n");
                    if(line.toLowerCase().contains("[msg]:ok")){
                        logger.info("------------转换成功[" + count + "]------------");
                        tempResult.get().set(true);
//                        break;
                    }
                }
            }catch (Exception e){
                logger.error(e.getMessage());
            }
        }).start();
        new Thread(() -> {
            BufferedReader error = new BufferedReader( new InputStreamReader(process.getErrorStream()));
            String line;
            try {
                while ((line = error.readLine()) != null) {
                    transInfo.append(line);
                    transInfo.append("\n");
                    if(line.toLowerCase().contains("[msg]:ok")){
                        logger.info("------------转换成功[" + count + "]------------");
                        tempResult.get().set(true);
//                        break;
                    }
                }
            }catch (Exception e){
                logger.error(e.getMessage());
            }
        }).start();
        //等待转换子进程结束
        try {
            process.waitFor();
        } catch (InterruptedException e) {
            logger.error(e.getMessage());
        }
        //销毁转换进程
        process.destroy();
        //检测转换结果
        boolean result = tempResult.get().get();
        if(!result) {
            logger.error("------------转换失败[" + count + "]------------");
            //日志记录转换信息
            logger.error("------------转换信息[" + count + "]------------");
            logger.info(transInfo.toString());
            if(count == 1) {
                result = runcmdCloud(cmd, destfile, 2);
            }
        }else {
            //日志记录转换信息
            logger.error("------------转换信息[" + count + "]------------");
            logger.info(transInfo.toString());
        }
        //返回结果
        return result;
    }
}
